{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "cellType": "markdown",
    "uuid": "c25b3fc6-dcec-44d8-8896-8593689c2934"
   },
   "source": [
    "## OSS使用案例\n",
    "\n",
    "这个是一个简单教程，告诉如何结合OSS和DSW，利用OSS来保存管理自己海量数据，然后利用DSW进行机器学习，从而再也不用担心自己数据和代码的丢失。\n",
    "\n",
    "### 在tensorflow如何访问OSS\n",
    "\n",
    "我们已经在https://github.com/tensorflow/io\n",
    "加入对于OSS的支持，然后你填入自己access_id和access_key, 就可以利用tensorflow的gfile来读取oss上的数据了。在这个例子，首先你应该在阿里云开通自己的OSS，然后在oss的控制台建立一个自己的bucket，在建立bucket记得选择好自己数据存放的区域，然后记录自己的endpoint，如下图\n",
    "![endpoint](./resource/oss0.jpg)\n",
    "\n",
    "然后去自己的阿里云用户总控制台获得自己的AK（access_id和access_key), 如下图\n",
    "![ak](./resource/oss1.jpg)\n",
    "\n",
    "在代码中填入这些信息，这样就能够使用tensorflow的gfile来访问自己在oss中的数据了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "cellType": "code",
    "uuid": "a050021d-0cc3-45db-97ac-c137cba25b9f"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['t10k-images-idx3-ubyte.gz', 't10k-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz', 'train-labels-idx1-ubyte.gz']\n"
     ]
    }
   ],
   "source": [
    "from __future__ import absolute_import\n",
    "from __future__ import division\n",
    "from __future__ import print_function\n",
    "\n",
    "import os\n",
    "import unittest\n",
    "\n",
    "from tensorflow.python.platform import test\n",
    "from tensorflow.python.platform import gfile\n",
    "import tensorflow_io as tfio  # pylint: disable=unused-import\n",
    "endpoint = 'oss-cn-zhangjiakou.aliyuncs.com'\n",
    "bucket = 'paidemos'\n",
    "access_id = 'xxxx'\n",
    "access_key = 'xxxxx'\n",
    "content = gfile.ListDirectory(\"oss://%s\\x01id=%s\\x02key=%s\\x02host=%s/\"\n",
    "                                  %(bucket, access_id, access_key, endpoint) + \"MNIST_data\")\n",
    "\n",
    "print(content)\n",
    "                                "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cellType": "markdown",
    "uuid": "2805cc1d-f0a0-4e62-8f5a-e9e1ee0ba8aa"
   },
   "source": [
    "### 如何利用oss本身的python SDK访问OSS数据\n",
    "\n",
    "这里oss2.auth(access_id,access_key)来构建authentication，然后就可以利用oss的sdk来访问自己数据，具体oss python sdk的文件请浏览https://help.aliyun.com/document_detail/52834.html\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "uuid": "74ffeafc-1df7-4a36-9ead-4967be9fc7b7"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MNIST_data/\n",
      "MNIST_data/t10k-images-idx3-ubyte.gz\n",
      "MNIST_data/t10k-labels-idx1-ubyte.gz\n",
      "MNIST_data/train-images-idx3-ubyte.gz\n",
      "MNIST_data/train-labels-idx1-ubyte.gz\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<oss2.models.GetObjectResult at 0x7fc52d33f438>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import oss2\n",
    "endpoint = 'oss-cn-zhangjiakou.aliyuncs.com'\n",
    "bucket = 'paidemos'\n",
    "auth = oss2.Auth('xxxx', 'xxxx')\n",
    "b = oss2.Bucket(auth, endpoint, bucket)\n",
    "for obj in oss2.ObjectIterator(b):\n",
    "    print(obj.key)\n",
    "\n",
    "b.get_object_to_file('MNIST_data/t10k-images-idx3-ubyte.gz', '1.gz')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "uuid": "22a8a90d-08b9-4ecf-ba2c-46b481b33025"
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
